package com.dropdb.demo.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

@Configuration
@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class SystemPermissionsConfig {

    @Bean(name = "sysPmConfig")
    public SystemPermissionsConfig systemPermissionsConfig() {
        Yaml yaml = new Yaml(new Constructor(SystemPermissionsConfig.class));
        SystemPermissionsConfig spc = null;
        try {
            spc = yaml.load(new FileInputStream(ResourceUtils.getFile("classpath:system-permissions.yml")));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return spc;
    }

    private List<ModulePermissions> systemPermissions;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class ModulePermissions {
        List<SubModulePermissions> children;
        String id;
        String label;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class SubModulePermissions {
        List<LeafPermission> children;
        String id;
        String label;
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class LeafPermission {
        String id;
        String label;
        String api;
    }
}
